package com.jtbd.service.impl;

import com.jtbd.config.OrganizationConfig;
import com.jtbd.constants.*;
import com.jtbd.domain.Organization;
import com.jtbd.domain.UserDepartmentRelation;
import com.jtbd.domain.UserEmployee;
import com.jtbd.domain.UserInfo;
import com.jtbd.dto.OrgCreateHttpDto;
import com.jtbd.dto.db.UserEmployeeInfo;
import com.jtbd.service.OrgManagementService;
import com.jtbd.service.OrganizationService;
import com.jtbd.service.UserEmployeeService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

@Service
public class OrgManagementServiceImpl implements OrgManagementService {
    @Resource
    private OrganizationService organizationService;
    @Resource
    private UserEmployeeService userEmployeeService;
    @Resource
    private OrganizationConfig organizationConfig;

    @Override
    @Transactional
    public Long addOrganization(OrgCreateHttpDto orgCreateHttpDto) {
        Organization organization = createOrganizationFromHttpDto(orgCreateHttpDto);
        organizationService.addOrganization(organization);
        UserEmployeeInfo orgAdminUser = createOrgAdminUser(orgCreateHttpDto.getContactPhone(), organization.getId());
        userEmployeeService.addUserEmployee(orgAdminUser);
        return organization.getId();
    }

    /**
     * 根据组织创建HTTP DTO创建组织实体对象
     *
     * @param orgCreateHttpDto 组织创建HTTP DTO，包含组织的基本信息
     * @return 创建的组织实体对象
     */
    private Organization createOrganizationFromHttpDto(OrgCreateHttpDto orgCreateHttpDto) {
        Organization organization = new Organization();
        organization.setName(orgCreateHttpDto.getName());
        organization.setOrgContact(orgCreateHttpDto.getContactPerson());
        organization.setOrgPhone(orgCreateHttpDto.getContactPhone());
        organization.setDescription(orgCreateHttpDto.getDescription());
        organization.setOrgJoinTime(orgCreateHttpDto.getJoinTime());
        organization.setOrgExprieTime(orgCreateHttpDto.getExprieTime());

        // 设置父级组织ID，如果DTO中未指定则使用默认的集团组织ID
        if (ObjectUtils.isEmpty(orgCreateHttpDto.getParentId())) {
            organization.setParentId(organizationConfig.getJtbdOrganization().getId());
        } else {
            organization.setParentId(orgCreateHttpDto.getParentId());
        }
        return organization;
    }


    /**
     * 创建校区管理员用户
     *
     * @param phone 手机号码
     * @param orgId 校区ID
     * @return UserEmployeeInfo 用户员工信息对象
     */
    private UserEmployeeInfo createOrgAdminUser(String phone, Long orgId) {
        // 创建用户员工信息对象
        UserEmployeeInfo userEmployeeInfo = new UserEmployeeInfo();
        UserInfo userInfo = new UserInfo();
        userInfo.setPassword(CommonConstant.USER_DEFAULT_PASSWORD);
        userInfo.setPhone(phone);
        userInfo.setUserType(UserTypeEnum.EMPLOYEE.getCode());
        userInfo.setUsername("机构管理员");
        userEmployeeInfo.setUserInfo(userInfo);

        // 设置员工信息
        UserEmployee employee = new UserEmployee();
        employee.setOrgId(orgId);
        employee.setEmployeeDepartmentIdentity(EmployeeDepartmentIdentityEnum.DEPARTMENT_LEADER.getCode());
        employee.setEmployeeStatus(StatusConstant.NORMAL);
        userEmployeeInfo.setUserEmployee(employee);
        return userEmployeeInfo;
    }

}
